package cn.vivi.user.service.impl;

import cn.vivi.common.entity.User;
import cn.vivi.user.entity.vo.LoginVo;
import cn.vivi.user.mapper.UserMapper;
import cn.vivi.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(LoginVo vo) {
        if (vo.getNickname() == null) {
            return this.findByEmail(vo.getEmail(), vo.getPassword());
        }
        return this.findByNickName(vo.getNickname(), vo.getPassword());
    }

    private User findByNickName(String nickname, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("nickname", nickname).eq("password", password);
        return userMapper.selectOne(wrapper);
    }

    private User findByEmail(String email, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email).eq("password", password);
        return userMapper.selectOne(wrapper);
    }

}
